package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.data.TagMapper;
import io.xxx.xbutler.domain.Callback;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 删除标签回调
 *
 * <pre>
 * {
 *     "nType": 2004,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": [
 *         {
 *             "nTagId": 1,
 *             "vcTagName": "",
 *             "vcBase64TagName": ""
 *         }
 *     ]
 * }
 * </pre>
 */
@Slf4j
@Component
public class _2004Handler implements CallbackHandler {

    @Resource
    private TagMapper tagMapper;

    @Override
    public String name() {
        return "删除标签回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 2004;
    }

    @Override
    public void handle(Callback callback) {
        JSONArray data = (JSONArray) callback.getData();
        List<Long> tagIds = data.stream()
                .map(it -> {
                    JSONObject info = JSONObject.from(it);
                    return info.getLong("nTagId");
                })
                .toList();
        tagMapper.deleteByIds(tagIds);
    }
}
